package com.sky.proxy.jdkDynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 演唱会
 *
 * @author : wushikai
 * <p>
 * date : 2020-12-11
 */
public class ConcertGym {


    public static void main(String[] args) {

        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        //public final class $Proxy0 extends Proxy implements IUser {
        IStar proxy = (IStar) Proxy.newProxyInstance(
                Thread.currentThread().getContextClassLoader(),
                new Class[]{IStar.class},
                new StarInvocationHandler<IStar>(new Singer("周杰伦"))   //mark 代理对象. 这个对象里面就是  InvocationHandler 的处理逻辑
        );

        proxy.sing("七里香");

        System.out.println("真正的替身是:" + proxy.getClass().getCanonicalName());
        System.out.println("替身的位置是:" + proxy.getClass().getPackage());


        try {
            IStar proxy1 = (IStar) Proxy.newProxyInstance(
                    Thread.currentThread().getContextClassLoader(),
                    new Class[]{IStar.class},
                    new StarInvocationHandler<>(new NetSinger("许嵩"))
            );
            proxy1.sing("半城烟沙");
        } catch (Exception e) {
            System.out.println("ERROR   cause  " + e.getMessage());
            e.printStackTrace();
        }
        IStar proxy2 = (IStar) Proxy.newProxyInstance(
                Thread.currentThread().getContextClassLoader(),
                new Class[]{IStar.class},
                new MybatisProxyGenerator(new NetSinger("许嵩"))
                //NetSinger  和  IStar  一毛钱关系没有;   这个 proxy 代理的方法传入的 method 参数 是 IStar 接口的参数,  但是你用不用, 就是另一回事
        );
        proxy2.sing("半城烟沙");


    }

    public static void raw() {
        IStar proxy = (IStar) Proxy.newProxyInstance(
                Thread.currentThread().getContextClassLoader(),
                new Class[]{IStar.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        return method.invoke(new Singer("周杰伦"), args);
                    }
                });
        proxy.sing("珊瑚海");

    }


}
